package com.ljf.tijian.service;

import com.ljf.tijian.ApiResponse;
import com.ljf.tijian.Utils.SmsUtils;
import com.ljf.tijian.exception.TijianAppException;
import com.ljf.tijian.exception.TijianExceptionEnum;
import com.ljf.tijian.mapper.UsersMapper;
import com.ljf.tijian.pojo.Orders;
import com.ljf.tijian.pojo.Sms.SMS;
import com.ljf.tijian.pojo.Users;
import com.ljf.tijian.service.Impl.UserServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.ibatis.jdbc.Null;
import org.bouncycastle.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserService implements UserServiceImpl {

    @Autowired
    UsersMapper usersMapper;

    @Autowired
    SmsUtils smsUtils;

    @Value("${salt}")
    String salt;

    public ApiResponse login(Users user){

        Users userRes = usersMapper.selectByPrimaryKey(user.getUserId());

        if(userRes == null){
//            return new ApiResponse(201,"该用户还未注册");
//            throw new TijianAppException(201,"用户还未注册");
            throw new TijianAppException(TijianExceptionEnum.MOBILE_NOT_REGISTER.getCode(),TijianExceptionEnum.MOBILE_NOT_REGISTER.getMessage());
        }else{
//            String salt = "nvfkahh321dsa";
            String originl_pwd = user.getPassword()+salt;
            String final_pwd = DigestUtils.md5Hex(originl_pwd.getBytes());
            if(userRes.getPassword().equals(final_pwd)){
                return new ApiResponse(200,"LOGIN SUCCESS",userRes);
            }else {
//                return new ApiResponse(202,"密码错误");
//                throw new TijianAppException(202,"密码错误");
                throw new TijianAppException(TijianExceptionEnum.PASSWORD_IS_WRONG.getCode(),TijianExceptionEnum.PASSWORD_IS_WRONG.getMessage());
            }
        }
    }

    public ApiResponse register(Users user){
        Users users = usersMapper.selectByPrimaryKey(user.getUserId());
        if(users != null){
            return new ApiResponse(TijianExceptionEnum.MOBILE_HAS_USED.getCode(),TijianExceptionEnum.MOBILE_HAS_USED.getMessage());
        }else{
            String originl_pwd = user.getPassword()+salt;
            String final_pwd = DigestUtils.md5Hex(originl_pwd.getBytes());
            user.setPassword(final_pwd);
            usersMapper.insert(user);
            return new ApiResponse(200,"REGISTER SUCCESS",users);
        }

    }

    public ApiResponse sendSms(Map<String, String> map) {
        String Phone = map.get("phone");
        Users users = usersMapper.selectByPrimaryKey(Phone);
        if(users == null){
            return new ApiResponse(TijianExceptionEnum.MOBILE_NOT_REGISTER.getCode(), TijianExceptionEnum.MOBILE_NOT_REGISTER.getMessage());
        }else{
            String sms = smsUtils.generateSms(Phone);
            if(sms.isEmpty()){
                return new ApiResponse(TijianExceptionEnum.SMS_FAILURE.getCode(),TijianExceptionEnum.SMS_FAILURE.getMessage());
            }else {
                return new ApiResponse(200,"SUCCESS",sms);
            }
        }
    }


    public ApiResponse smsLogin(SMS sms){
        Users users = usersMapper.selectByPrimaryKey(sms.getPhone());
        if(users == null){
            return new ApiResponse(TijianExceptionEnum.MOBILE_NOT_REGISTER.getCode(), TijianExceptionEnum.MOBILE_NOT_REGISTER.getMessage());
        }else{
            boolean result = smsUtils.validateSms(sms.getPhone(), sms.getSms());
            if(result == false){
                return new ApiResponse(TijianExceptionEnum.SMS_LOGIN_FAILURE.getCode(), TijianExceptionEnum.SMS_LOGIN_FAILURE.getMessage());
            }else{
                return new ApiResponse(200,"SUCCESS",users);
            }
        }
    }

}
